package com.cyj.qqclient.service;
/**
 * 提供和消息相关的方法
 */

import com.cyj.qqcommon.Message;
import com.cyj.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class MessageClientService implements Serializable {
    //私聊
    public static void sendMessageToOne(String content, String senderId, String getterId) {
        Message message = new Message();
        message.setMessageType(MessageType.MESSAGE_COMM_MES);
        message.setSender(senderId);
        message.setContent(content);
        message.setGetter(getterId);
        message.setSendtime(new Date().toString());
        System.out.println(senderId+"对"+getterId+"说："+content);
        //发送给服务端
        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream(ManageClientConnectSeverThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //群发
    public static void sendMessageToALL(String content, String senderId) {
        Message message = new Message();
        message.setMessageType(MessageType.MESSAGE_TOALL_MES);
        message.setSender(senderId);
        message.setContent(content);
        message.setSendtime(new Date().toString());
        System.out.println(senderId+"对"+"大家说："+content);
        //发送给服务端
        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream(ManageClientConnectSeverThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
